package com.yz.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yz.entity.Dailylog;
import com.yz.entity.Student;
import com.yz.entity.Teacher;
import com.yz.entity.dto.DailylogDto;
import com.yz.service.DailylogService;
import com.yz.service.StatisticService;
import com.yz.service.StudentService;
import com.yz.service.TeacherService;
import com.yz.util.Result;

@RestController
public class DailylogController {
	@Autowired
	private DailylogService dailylogService;
	@Autowired
	StatisticService statisticService;
	@Autowired
	StudentService studentService;
	@Autowired
	TeacherService teacherService;
	
	@RequestMapping("/getInfoById")
	public Object getInfoById(int id,int type) {
		if(type==0) {
			Student stuById = studentService.getStuById(id);
			if(stuById!=null) {
				return new Result(0,"",stuById);
			}else {
				return new Result(1,"此用户不存在",null);
			}
		}else {
			Teacher teaById = teacherService.getTeaById(id);
			if(teaById!=null) {
				return new Result(0,"",teaById);
			}else {
				return new Result(1,"此用户不存在",null);
			}
		}
	}
	
	
	
	
	
	
	@RequestMapping("/getLogList")
	public Object getLogList(DailylogDto dailylogDto,Dailylog dailylog) {
		List<Dailylog> list = dailylogService.getLog(dailylogDto);
		dailylogDto.setPage(null);
		dailylogDto.setLimit(null);
		List<Dailylog> list2 = dailylogService.getLog(dailylogDto);
		return new Result(0,"",list2.size(),list);
	}
	
	@RequestMapping("/add")
	public Object add(String scardNo,Integer readRoomId) {
		Dailylog dl=new Dailylog();
		dl.setCardNo(scardNo);
		dl.setReadRoomId(readRoomId);
		System.out.println(dl);
		List<DailylogDto> checkUser = dailylogService.checkUser(scardNo);
		System.out.println(checkUser.size());
		if(checkUser.size()<=0) {
			if(dailylogService.add(dl)) {
				//更新访问资源人数
				statisticService.updateStaInfoNum(readRoomId);
				return new Result(0,"打卡成功");
			}else {
				return new Result(1,"打卡失败");
			}
		}else {
			return new Result(1,"打卡失败,此用户正在占用其他资源");
		}
		
	}
	@RequestMapping("/add1")
	public Object add1(String tcardNo,Integer readRoomId) {
		Dailylog dl=new Dailylog();
		dl.setCardNo(tcardNo);
		dl.setReadRoomId(readRoomId);
		System.out.println(dl);
		List<DailylogDto> checkUser = dailylogService.checkUser(tcardNo);
		System.out.println(checkUser.size());
		if(checkUser.size()<=0) {
			if(dailylogService.add(dl)) {
				//更新访问资源人数
				statisticService.updateStaInfoNum(readRoomId);
				return new Result(0,"打卡成功");
			}else {
				return new Result(1,"打卡失败");
			}
		}else {
			return new Result(1,"打卡失败,此用户正在占用其他资源");
		}
		
	}
	
	@RequestMapping("/update")
	public Object update(int logid) {
		Dailylog d=new Dailylog();
		d.setLogid(logid);
		if(dailylogService.update(d)) {
			return new Result(0,"打卡退出成功");
		}else {
			return new Result(1,"打卡退出失败");
		}
	}
	
	@RequestMapping("/getAllRead1")
	public Object getAllRead1() {
		return new Result(0,"",dailylogService.getAllRead());
	}
}
